import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/components/Home'

import Login from '@/components/login'//登录页

import Journal from '@/components/journal/journal' //日志

import State from '@/components/state/state' //设置
import Statistics from '@/components/Statistics/Statistics' //全屏表单
import ResetPassword from '@/components/resetpassword'


Vue.use(Router)

const router = new Router({
  linkActiveClass: 'tab-bg',
  routes: [
    { path: '/', redirect: '/Statistics'},
    {
      path: '/Home', name: 'Home', component: Home , 
      children:[
      { path: '/Journal', component: Journal, name: '日志', hidden: true },
      { path: '/State', component: State, name: '设置'  } 
    ]
  },
    { path: '/login', component:Login}, //登录    
    { path: '/Statistics', component: Statistics, name: '查看大图' },
    { path: '/ResetPassword', component: ResetPassword, name: '修改密码' } 

  ]
})

//全局路由守卫
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!sessionStorage.getItem('token')) {
      next({
        path: '/login',
        query: {
          redirect: to.fullPath
        }
      })
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router